<?php
namespace app\shop\controller;
use app\AdminRest;
use app\farm\model\ShopGoods;
use app\shop\model\Article;
use app\shop\model\Banner;
use app\shop\model\Date;
use app\shop\model\Goods;
use app\shop\model\Integral;
use app\shop\model\IntegralList;
use app\shop\model\LuckDraw;
use app\shop\model\OrderGoods;
use app\shop\model\RefundOrder;
use app\shop\model\SeckillConfig;
use app\shop\model\SeckillGoods;
use app\shop\model\SeckillList;
use app\shop\model\Signin;
use app\shop\model\Wallet;
use think\App;
use app\shop\model\User as Model;


class AdminMark extends AdminRest
{


    protected $model;

    protected $order_goods_model;

    protected $refund_order_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new IntegralList();

    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-29 13:43
     * @功能说明:积分列表
     */
    public function integralList(){

        $this->model->initAtv();

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status','>',-1];

        if(!empty($input['goods_name'])){

            $dis[] = ['b.name','like','%'.$input['goods_name'].'%'];
        }

        if(!empty($input['atv_status'])){

            $dis[] = ['a.atv_status','=',$input['atv_status']];
        }

        $data = $this->model->dataGoodsList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-29 11:58
     * @功能说明:添加积分商城规则
     */
    //[{"spe_id":1,"stock":1,"price":1,"integral":1}]
    public function integralAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $res = $this->model->dataAdd($input);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-29 13:28
     * @功能说明:积分活动详情
     */
    public function integralInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        $goods_model = new ShopGoods();

        $data['goods_name'] = $goods_model->where(['id'=> $data['goods_id']])->value('goods_name');

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-29 13:30
     * @功能说明:编辑积分规则
     */
    public function integralUpdate(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $res = $this->model->dataUpdate(['id'=>$input['id']],$input);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-15 11:54
     * @功能说明:签到详情
     */
    public function signInfo(){

        $dis = [

            'uniacid' => $this->_uniacid
        ];

        $sign_model = new Signin();

        $data = $sign_model->dataInfo($dis);

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2022-07-15 11:54
     * @功能说明:签到编辑
     */
    public function signUpdate(){

        $input = $this->_input;

        $dis = [

            'uniacid' => $this->_uniacid
        ];

        $sign_model = new Signin();

        $data = $sign_model->dataUpdate($dis,$input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-15 14:17
     * @功能说明:抽奖活动列表
     */
    public function luckList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        if(!empty($input['atv_status'])){

            $dis[] = ['atv_status','=',$input['atv_status']];

        }

        $luck_model = new LuckDraw();

        $luck_model->initAtv();

        $data = $luck_model->dataList($dis,$input['limit']);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-15 14:18
     * @功能说明:添加抽奖
     */
    public function luckAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $luck_model = new LuckDraw();

        if(!empty($input['data'])){

            $balance = array_sum(array_column($input['data'],'balane'));

            if($balance>100||$balance<0){

                $this->errorMsg('累计中奖率在0-100%之间');
            }

        }

        $check = $luck_model->checkTime($input);

        if(!empty($check['code'])){

            $this->errorMsg($check['msg']);

        }

        $res = $luck_model->dataAdd($input);

        return $this->success($res);

    }




    /**
     * @author chenniang
     * @DataTime: 2022-07-15 14:18
     * @功能说明:添加抽奖
     */
    public function luckUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        if(!empty($input['data'])){

            $balance = array_sum(array_column($input['data'],'balane'));

            if($balance>100||$balance<0){

                $this->errorMsg('累计中奖率在0-100%之间');
            }

        }
        $luck_model = new LuckDraw();

        $check = $luck_model->checkTime($input);

        if(!empty($check['code'])){

            $this->errorMsg($check['msg']);

        }

        $res = $luck_model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-15 14:19
     * @功能说明:抽奖详情
     */
    public function luckInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $luck_model = new LuckDraw();

        $res = $luck_model->dataInfo($dis);

        return $this->success($res);

    }



    /**
     * @author chenniang
     * @DataTime: 2022-07-15 14:17
     * @功能说明:秒杀活动列表
     */
    public function killList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        if(!empty($input['atv_status'])){

            $dis[] = ['atv_status','=',$input['atv_status']];
        }

        $luck_model = new SeckillList();

        $luck_model->initAtv();

        $data = $luck_model->dataList($dis,$input['limit']);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['order_price'] = 0;

                $v['order_count'] = 0;

                $v['user_count']  = 0;

            }
        }

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-15 14:18
     * @功能说明:添加秒杀
     */
    public function killAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $luck_model = new SeckillList();

        $res = $luck_model->atvCheck($input);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        $res = $luck_model->dataAdd($input);

        return $this->success($res);

    }




    /**
     * @author chenniang
     * @DataTime: 2022-07-15 14:18
     * @功能说明:编辑秒杀
     */
    public function killUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $luck_model = new SeckillList();

        if(isset($input['start_time'])&&isset($input['end_time'])){

            $res = $luck_model->atvCheck($input);

            if(!empty($res['code'])){

                $this->errorMsg($res['msg']);
            }

        }

        $res = $luck_model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-15 14:19
     * @功能说明:秒杀详情
     */
    public function killInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $luck_model = new SeckillList();

        $res = $luck_model->dataInfo($dis);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-25 14:22
     * @功能说明:添加秒杀商品
     */
    public function addKillGoods(){

        $input = $this->_input;

        $luck_model = new SeckillList();

        $res = $luck_model->updateSome($input['atv_id'],$this->_uniacid,$input['goods_info'],$input['goods_id']);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-25 14:58
     * @功能说明:秒杀商品列表
     */
    public function killGoodsList(){

        $input = $this->_param;

        $goods_model = new SeckillGoods();

        $dis[] = ['a.atv_id','=',$input['atv_id']];

        $dis[] = ['b.status','=',1];

        $dis[] = ['a.status','=',1];

        $dis[] = ['d.status','=',2];

        if(!empty($input['goods_name'])){

            $dis[] = ['b.goods_name','like','%'.$input['goods_name'].'%'];

        }

        $data = $goods_model->goodsList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-25 15:13
     * @功能说明:删除秒杀商品
     */
    public function killGoodsDel(){

        $input = $this->_input;

        $goods_model = new SeckillGoods();

        $dis = [

            'id' => $input['id']
        ];

        $res = $goods_model->where($dis)->delete();

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-25 15:23
     * @功能说明:秒杀商品详情
     */
    public function killGoodsInfo(){

        $input = $this->_param;

        $goods_model = new SeckillGoods();

        $dis = [

            'atv_id' => $input['atv_id'],

            'goods_id'=> $input['goods_id'],
        ];

        $res = $goods_model->dataInfo($dis);

        $goods_model = new ShopGoods();

        $res['goods_name'] = $goods_model->where(['id'=> $input['goods_id']])->value('goods_name');

        return $this->success($res);


    }







}
